package com.henry.album.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.henry.album.files.manager.FileInfoManager;
import com.henry.album.files.model.FileInfo;
import com.henry.album.service.FileScanService;

@RestController

@RequestMapping(value = "/file")
public class MyFileController {

	@Resource
	FileScanService fileScanService;

	@Autowired
	FileInfoManager fileInfoManager;

	/**
	 * 系统第一次初始化目录到数据库
	 * 
	 * @throws Exception
	 */
	@GetMapping(value = "/initPath")
	public void initPath() throws Exception {
		fileScanService.scan();
	}

	/**
	 * 根据目录查找子项
	 * 
	 * @param path
	 * @return
	 */
	@PostMapping(value = "/getPath")
	@ResponseBody
	public List<FileInfo> getPath(String path) {

		String headerStr = "";// 默认头
		String os = System.getProperty("os.name");
		if (os.startsWith("Windows")) {
			headerStr = "D:/data/";
		} else {
			headerStr = "/data/";
		}
		List<FileInfo> list = fileInfoManager.getParentList(headerStr + path);
		// List<FileInfo> list = fileInfoManager.getParentList("D:/data/aaa/");
		return list;

	}

	/**
	 * 图片下载
	 * 
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@GetMapping(value = "/downloadById/{fileId}")
	public void downloadById(@PathVariable("fileId") String fileId, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 要下载的图片地址
		// String path = request.getServletContext().getRealPath("/upload");

		FileInfo fileInfo = fileInfoManager.getById(fileId);

		if (fileId == null) {
			return;
		}

		// 1、设置response 响应头
		response.reset(); // 设置页面不缓存,清空buffer
		response.setCharacterEncoding("UTF-8"); // 字符编码
		response.setContentType("multipart/form-data"); // 二进制传输数据
		// 设置响应头
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(fileInfo.getFileName(), "UTF-8"));
		File file = new File(fileInfo.getAbsolutePath());
		// 长度
		response.setContentLength((int) file.length());
		// 2、 读取文件--输入流
		InputStream input = new FileInputStream(file);
		// 3、 写出文件--输出流
		OutputStream out = response.getOutputStream();
		byte[] buff = new byte[1024 * 2];
		int index = 0;
		// 4、执行 写出操
		while ((index = input.read(buff)) != -1) {
			out.write(buff, 0, index);
			out.flush();
		}
		out.close();
		input.close();

	}

	/**
	 * 图片下载
	 * 
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@GetMapping(value = "/download/{fileName}")
	public void download_(@PathVariable("fileName") String fileName, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 要下载的图片地址
		// String path = request.getServletContext().getRealPath("/upload");

		// 1、设置response 响应头
		response.reset(); // 设置页面不缓存,清空buffer
		response.setCharacterEncoding("UTF-8"); // 字符编码
		response.setContentType("multipart/form-data"); // 二进制传输数据
		// 设置响应头
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

		File file = new File("D:/aaa", fileName);
		
		if(!file.exists()) {return;}

		// 长度
		response.setContentLength((int) file.length());

		// 2、 读取文件--输入流
		InputStream input = new FileInputStream(file);
		// 3、 写出文件--输出流
		OutputStream out = response.getOutputStream();

		byte[] buff = new byte[1024 * 2];
		int index = 0;
		// 4、执行 写出操
		while ((index = input.read(buff)) != -1) {
			out.write(buff, 0, index);
			out.flush();
		}
		out.close();
		input.close();

	}

}
